ABC285 D - Change Usernames
https://atcoder.jp/contests/abc285/tasks/abc285_d
提出
code: python
from collections import deque
from collections import defaultdict
n = int(input())
st = list(input().split()) for _ in range(n)
changed = set()
d = defaultdict(str)
# どこかで循環があったらダメ
for s, t in st:
ds = t
q = deque([d[st00]])
changed.add(st00)
for k in list(d.keys()):
while q:
next = q.popleft()
if dnext != "":
if next in changed:
print("No")
exit()
changed.add(next)
q.append(dnext)
if k not in changed:
q.append(dk)
print("Yes")
解答
code: python
from collections import defaultdict
n = int(input())
st = list(input().split()) for _ in range(n)
# print(st)
# 'aaa', 'bbb'], 'yyy', 'zzz', 'ccc', 'ddd', 'xxx', 'yyy', ['bbb', 'ccc'
d = defaultdict(str)
start = []
for s, t in st:
ds = t
start.append(s)
# print(d)
# {'aaa': 'bbb', 'yyy': 'zzz', 'ccc': 'ddd', 'xxx': 'yyy', 'bbb': 'ccc'}
# print(start)
# 'aaa', 'yyy', 'ccc', 'xxx', 'bbb'
visited = set()
for start_step in start:
next_step = dstart_step
# 既に調べたループが無くなるまで
while next_step not in visited:
visited.add(next_step)
if next_step == "":
break
# 更新
next_step = dnext_step
# 循環が発生している
if next_step == start_step:
print("No")
exit()
print("Yes")
テーマ
#bfs
メモ
AtCoder Beginner Contest 285 A~D問題Python解説
提出
code: python
from collections import defaultdict
from collections import deque
n = int(input())
st = list(input().split()) for _ in range(n)
# 循環
d = defaultdict(str)
visited = defaultdict(bool)
for s, t in st:
ds = t
visiteds = False
visitedt = False
print(d)
# defaultdict(<class 'str'>, {'aaa': 'bbb', 'yyy': 'zzz', 'ccc': 'ddd', 'xxx': 'yyy', 'bbb': 'ccc'})
# now = st00
# visitednow = True
for k, v in d.items():
if not visited[dk]:
now = k
visitednow = True
while True:
next = dnow
if visitednext:
print("No")
exit()
visitednext = True
if not next:
break
else:
now = dnext